import greenfoot.*;

/**
 * A rocket that can be controlled by the arrowkeys: up, left, right.
 * The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
 * 
 * @author Poul Henriksen
 * @author Michael Kölling
 * 
 * @version 1.1
 */
public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;// The minimum delay between firing the gun.
    private static final int protonReloadTime =20;
    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    private int protonDelayCount;
    private GreenfootImage rocket = new GreenfootImage("rocket.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
    
    /**
     * Initialise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        protonDelayCount = 20;
    }
    
    /**
     * Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
     * accelerating and shooting when the right keys are pressed.)
     */
    public void act()
    {
        checkKeys();
        reloadDelayCount++;
        protonDelayCount++;
        move();
    }
    
    public void checkKeys(){
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
        if (Greenfoot.isKeyDown("left")) 
        {
            turn(-10);
            fashe1();
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            turn(10);
            fashe1();
        }
        if (Greenfoot.isKeyDown("up")) 
        {
            getOneObjectAtOffset(100,100,Rocket.class);
            move(3);
            fashe();
        }
        else{
           fashe();
        }
        if (Greenfoot.isKeyDown("z")) 
        {
            fireProton();
        }
    }
    
    public void fashe(){
        setImage("rocketWithThrust.png");
    }
    
    public void fashe1(){
        setImage("rocket.png");
    }
    /**
     * Check whether there are any key pressed and react to them.
     */
    
    
    private void fireProton() 
    {
        if (protonDelayCount >= protonReloadTime){
            ProtonWave wave = new ProtonWave(getVelocity(), getRotation());
            getWorld().addObject (wave, getX(), getY());
            protonDelayCount = 0;
        }
    }
    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Bullet bullet = new Bullet (getVelocity(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullet.move ();
            reloadDelayCount = 0;
        }
    }
}